<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.ionglobal.db.*" %>
<%@ page import="com.ionglobal.lib.*" %>
<%@ page import="com.ionglobal.misc.*" %>
<%@ page import="com.ionglobal.fms.*" %>
<%@ include file="../init_include.jsp" %>
<%
	String uploadpath=FmsEnv.getUploadPath();
	String uploadext=FmsEnv.getUploadFileExt();
	String disableext=FmsEnv.getDiableFileExt();

	Sysop sysop=(Sysop)session.getAttribute("sysop");
	if ( sysop==null)	{
		response.sendRedirect("../index.jsp");
		return;
	}	else {
		FmsFile ff=FmsFile.getSysop(dbi,sysop.getId());
		if(ff.getId()>0 || SmsEnv.checkSuperUser(sysop.getName())) {
			disableext=ff.getDisableExt();
			uploadext=ff.getFileExt();
			uploadpath=ff.getFolder();
		}		
	}			
	
	int fnLength=FmsEnv.getFileNameLength();;	
	String relativePath=request.getParameter("path");
	if(relativePath==null) relativePath="";	
	
	String module=request.getParameter("module");
	String rstUrl=request.getParameter("rstUrl");
	if(rstUrl==null)
		rstUrl="fmsSelectFile.jsp";

	if ( (module==null)||(module.length()<1)) module="fms";

	String fileRoot=SysEnv.get("web.fileRoot");
	String filetail=SysEnv.get(""+module+".fileRoot");

	while (fileRoot.endsWith("\\")||fileRoot.endsWith("/"))
		fileRoot=fileRoot.substring(0,fileRoot.length()-1);
	if (fileRoot.length()<1) fileRoot="/";
	filetail=Utilities.chargePathSeparator(filetail);
	if(filetail!=null && filetail.length()>0)
		fileRoot=fileRoot+File.separator+filetail;
	
	String rootUrl=SysEnv.get("web.fileUrl");
	String webtail=SysEnv.get(""+module+".fileUrl");
	while (rootUrl.endsWith("\\")||rootUrl.endsWith("/"))
		rootUrl=rootUrl.substring(0,rootUrl.length()-1);
	if (rootUrl.length()<1) 
		rootUrl="";
	else
		rootUrl="/"+rootUrl+"/";
	webtail=Utilities.chargePathSeparator(webtail);
	if(webtail!=null && webtail.length()>0)
		rootUrl=rootUrl+filetail+"/";
	
	String completePath;
	String smodule = (String)session.getAttribute("FMS_MODULE");	
	if( module == null || module.trim().length() == 0 ){
		module = "fms";
	}
	session.setAttribute("FMS_MODULE",module);

	if ( relativePath==null){
		if( session.getAttribute("FMS_RELATIVEPATH") != null && smodule!= null && module.equals(smodule))
			relativePath = (String)session.getAttribute("FMS_RELATIVEPATH");
		else 
			relativePath="";
	}
	else{
		while (relativePath.indexOf("../")!=-1)
			relativePath=Utilities.strReplace(relativePath,"../","");
		session.setAttribute("FMS_RELATIVEPATH", relativePath );
	}	
	
	relativePath=Utilities.chargePathSeparator(relativePath);
	completePath=fileRoot+File.separator+relativePath;
	
	String url=relativePath;
	url=url.replace('\\','/');

	String oldPath="";
	if(request.getParameter("oldPath")!=null)
		oldPath=request.getParameter("oldPath");
	else
		oldPath=completePath;
		
	String oldPath1=oldPath;
	oldPath=Utilities.strReplace(oldPath,"\\","\\\\");		
%>
<HTML>
<HEAD>
<link rel="stylesheet" href="../css.css" type="text/css">
<script>
<!--
var form1=this;
if(!parent)
	window.close();
	
function send(i)
{	
 	var top=this;
 	if (parent) top=parent;
 	
 	if (i==0) 
 	{
 		if (document.fileManage.which)
	 		top.returnValue=document.fileManage.which.value;
 		else
 			top.returnValue="";
 	}	else {	
 		top.returnValue=document.fileManage.which[i].value;
 	}
 	top.close(); 	
}
 
function goform(module,rstUrl,parentStr,oldpath)
{	
	location.href="fmsOpertionFile.jsp?module="+module+"&rstUrl="+rstUrl+"&path="+parentStr+"&oldPath="+oldpath;
}

function goselectform(module,rstUrl,parentStr,oldpath)
{	
	location.href="fmsSelectFile.jsp?module="+module+"&rstUrl="+rstUrl+"&path="+parentStr+"&oldPath="+oldpath;
}


function doRename(actUrl,j)
{
	if(document.fileManage.rename.value=="")
	{
		alert("<%=Language.getNameByCode("fms.file.rename.alert","Please input name first!")%>");
		document.fileManage.rename.focus();
		return false;
	}
	var fn='';
	var i=0;
	if(j>1) {
		while (document.fileManage.which[i])
		{
			if (document.fileManage.which[i].checked)
			{
				fn=document.fileManage.which[i].value;
				break;
			}
			i++;
		}
	} else
		fn=document.fileManage.which.value;
		
	if (fn!='')
	{
		document.fileManage.action=actUrl+'?ra='+fn;
		document.fileManage.submit();
	}
	else
		window.alert("<%=Language.getNameByCode("fms.alert","Please select one file or directory first!")%>");			
}

function doCreate(actUrl)
{
	if(document.fileManage.createCate.value=="")
	{
		alert("<%=Language.getNameByCode("fms.directorycreate.alert","Please input directory name first!")%>");
		document.fileManage.createCate.focus();
		return false;
	}
	document.fileManage.action=actUrl;
	document.fileManage.submit();
}

function confirmDel(j)
{	
	var i=0;
	var fn='';
 	if (j<=1) 
 	{
		fn=document.fileManage.which.value; 		
	} else {
		while (document.fileManage.which[i])
		{
			if (document.fileManage.which[i].checked)
			{
				fn=document.fileManage.which[i].value;
				break;
			}
			i++;
		}
	}
	if (fn!='')
	{
		var msg=<%=Language.getNameByCode("fms.alert.delete","'Will you delete '+fn+'?'")%>;
		if ( window.confirm(msg))
		{
			document.fileManage.action='fmsFile_del.jsp?ra='+fn;
			document.fileManage.submit();
		}
	}
	else
		window.alert("<%=Language.getNameByCode("fms.alert","Please select one file or directory first!")%>");			
}

function uploadCheck()
{
	if(document.picList.file.value=="") {
		alert("<%=Language.getNameByCode("fms.upload.alert","Please select your locate file first!")%>");
		document.picList.file.focus();
		return false;
	}		
	var fileext="<%=uploadext%>";
	var disablefileext="<%=disableext%>";
	var filestr=document.picList.file.value;
	var intPos=0;
	var intPos1=0;
	var tempext="";
	var str=fileext;
	if(fileext!=null && fileext!="") {
		intPos=str.indexOf("|");		
		while(intPos!=-1) {
			tempext=str.substring(0,intPos);
			intPos1=filestr.indexOf(tempext);
			if(intPos1!=-1)
				return true;	
			str=str.substring(intPos+1);
			intPos=str.indexOf("|");					
		}
		tempext=str;
		intPos1=filestr.indexOf(tempext);
		if(intPos1!=-1)
				return true;		
		alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
		return false;
	}	
	str=disablefileext;
	if(disablefileext!=null && disablefileext!="") {
		intPos=str.indexOf("|");		
		while(intPos!=-1) {
			tempext=str.substring(0,intPos);
			intPos1=filestr.indexOf(tempext);
			if(intPos1!=-1) {
			  alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
				return false;	
			}
			str=str.substring(intPos+1);
			intPos=str.indexOf("|");					
		}
		tempext=str;
		intPos1=filestr.indexOf(tempext);
		if(intPos1!=-1) {
				alert("<%=Language.getNameByCode("fms.upload.alert1","You cannot upload file with this type!")%>");
				return false;
		}						
	}	
	return true;
}
//-->
</script>
</HEAD>
<body>
<table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#666666">
<tr class="bgcolor_title_tr" >
<td>
<%
	String str="";
	str=Language.getNameByCode("sys.CurrentPos","Current Position")+":";
	str+=Utilities.strReplace(rootUrl + url,"//","/");
	out.println(str);
%>
&nbsp;&nbsp;
</td>
<td align="center">
<%
boolean canUpload=true,canManage=true;
if(true) {
	String parentStr=relativePath;
	if(File.separator.equals("\\"))
		parentStr=Utilities.strReplace(parentStr,"\\","\\\\");
%>
		--><a href="" onClick="goselectform('<%=module%>','<%=rstUrl%>','<%=parentStr%>','<%=oldPath%>');return false;"><%=Language.getNameByCode("fms.select","File Select")%></a>
<%}%>	
</td>
</tr>
<% if(canUpload) {%>
<form method="post" action="fmsFileUpload.jsp" name="picList" ENCTYPE="multipart/form-data" onSubmit="return uploadCheck();">
<tr class="bgcolor_title_tr" >
<td colspan="2">
<input type="hidden" name="filePath" value="<%=relativePath%>">
<input type="hidden" name="rstUrl" value="fmsOpertionFile.jsp">
<input type="hidden" name="oldPath" value="<%=oldPath1%>">
<input type="hidden" name="module" value="<%=module%>">
<%=Language.getNameByCode("fms.fileupload","File Upload")%><input type="file" name="file">&nbsp;<input type="submit" name="Submit" value="<%=Language.getNameByCode("fms.upload","Upload")%>">
</td>
</tr>
</form>
<tr class="bgcolor_title_tr">
<td colspan="2">
	<font color="#FF0000"><u><%=Language.getNameByCode("fms.uploadcomment","If your system isnot support double charset,please user filename with single charset!")%></u></font>
</td>
</tr>
<%} else {%>
<tr class="bgcolor_title_tr" >
<td colspan="2">
	<%=Language.getNameByCode("fms.noupload.right","You have no right!")%>
</td>
</tr>
<%}%>
</table>
<% if(canManage) {%>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<form method="post" action="" name="fileManage" >
	<input type="hidden" name="rstUrl" value="fmsOpertionFile.jsp">
	<input type="hidden" name="oldPath" value="<%=oldPath1%>">
  <tr>
  	<td width="8%"><%=Language.getNameByCode("sys.select","Select")%></td>
    <td><%=Language.getNameByCode("fms.name","File Name")%></td>
    <td align=center><%=Language.getNameByCode("fms.type","File Type")%></td>
    <td align=center><%=Language.getNameByCode("fms.size","File Size")%></td>
  </tr>
<%
	int pageno=0,pagesize=0;	
	ArrayList array=FileManager.getFiles(completePath,uploadext,disableext,pageno,pagesize);
	str="";
	String filePath="";
	int indexRa=-1;
	if(relativePath.length()>0)
	{
    File path=new File(completePath);	
		String parentStr=path.getParent();
		parentStr=parentStr.substring(fileRoot.length());
		if(!oldPath.equals(Utilities.strReplace(completePath,"\\","\\\\")))
		{
			indexRa++;
			if(File.separator.equals("\\"))
				parentStr=Utilities.strReplace(parentStr,"\\","\\\\");
		%>
		<tr style="background-color:#<%=((0%2)==0)?"cccccc":"FFcc33" %>">
    	<td>
        <input type="radio" name="which" value="<%=parentStr+"/." %>" >
    	</td>
    	<td><img src="icon/folder.jpg" ><a href="javascript:goform('<%=module%>','<%=rstUrl%>','<%=parentStr%>','<%=oldPath%>')"><%=Language.getNameByCode("fms.parent","..")%></a></td>
    	<td align=center><%=Language.getNameByCode("fms.directory","Directory")%></td>
    	<td align=center></td>
  		</tr>
  		<%}
  	}
  	if(array!=null) {
			for (int i=0;i<array.size();i++) {	
				FileItem fi=(FileItem)array.get(i);
				String childPath=relativePath+File.separator+fi.getFileName();
				if(File.separator.equals("\\"))
					childPath=Utilities.strReplace(childPath,"\\","\\\\");				
		%>
		<tr style="background-color:#<%=(((i+1)%2)==0)?"cccccc":"FFCC33" %>">
    	<td><input type="radio" name="which" value="<%=fi.getFileName()%>">
    	</td>
    	<td><img src="<%=fi.getIcon()%>" >
    	<%
    		if(fi.isDirectory()) {
    	%>
    		<a href="javascript:goform('<%=module%>','<%=rstUrl%>','<%=childPath%>','<%=oldPath%>')"><%=fi.getFileName()%></a>
    	<% } else {
					String encode=fi.getFileName();
//					encode=java.net.URLEncoder.encode(encode);
		
					String fileRef=rootUrl+"/"+url+"/"+encode;
					while (fileRef.indexOf("//")!=-1)
					fileRef=Utilities.strReplace(fileRef,"//","/");
			%>
				<a href="<%=fileRef%>" target="_blank"><%=fi.getFileName()%></a>
			<%}%>
				
    	</td>
    	<td align=center>
    		<% if(fi.isDirectory()) 
    					out.print(Language.getNameByCode("fms.directory","Directory"));
    			 else
    					out.print(fi.getType());    	    							 
    		%>
    	</td>
    	<td align=center><%=(fi.isDirectory()?"":fi.getSize()+"k")%></td>
  		</tr>
  		<%}
	}
%>    
	</table>
	
	<table width="100%" border="0">
<% 	if (array!=null && array.size()>0)
	{%>
	    <tr>
	      <td width="17%">
	        <input type="button" name="Submit4" value="<%=Language.getNameByCode("sys.Delete","Delete")%>" onClick="confirmDel(<%=array.size()%>)">
	      </td>
	      <td width="24%">(<i>*<%=Language.getNameByCode("fms.deletecommnet","Delete file or directory")%></i>)</td>
	      <td width="59%">
        <input type="button" name="Submit5" value="<%=Language.getNameByCode("sys.Submit","Submit")%>" onClick="">
      </td>
	    </tr>
	    <tr>
	      <td width="17%"><%=Language.getNameByCode("fms.renameto","Rename To")%>:</td>
	      <td width="24%"> 
	        <input type="text" name="rename">
	      </td>
	      <td width="59%"> 
	        <input type="button" name="Submit3" value="<%=Language.getNameByCode("fms.rename","Rename")%>" onClick="doRename('fmsFile_rename.jsp',<%=array.size()%>)">
	        (<i>*<%=Language.getNameByCode("fms.rename.comment","Rename file or directory.")%></i>) </td>
	    </tr>
	 <%}
%>   
    <tr>
      <td width="17%"><%=Language.getNameByCode("fms.create.subdirectory","Create sub-directory")%>:</td>
      <td width="24%"> 
        <input type="text" name="createCate">
      </td>
      <td width="59%"> 
        <input type="button" name="Submit2" value="<%=Language.getNameByCode("fms.create","Create")%>" onClick="doCreate('fmsFile_mkdir.jsp')">
        (<i><%=Language.getNameByCode("fms.create.comment","Create sub-directory in current directory!")%></i>) </td>
    </tr>
	 <input type="hidden" name="path" value="<%=relativePath%>">
	 <input type="hidden" name="module" value="<%=module%>">
	</form>
  </table>	
<%}%>
</body>
</HTML>
<%@ include file="../init_free.jsp" %>     

